module.exports = {
    config: {
        asRouteService: true,
        routeRoot: 'cloud',
        dependencies: [
        ],
    },
    i18n: {
        'en-us': {
            'module-title': 'Private Cloud',
            'module-description': '',
        },
        'zh-cn': {
            'module-title': '私有云',
            'module-description': '',
        },
    },
    hooks: {
        onModulesReady: async (app) => {
            // register flow actions
            app.addFlowActions([
                {
                    Flow: 'vm',
                    Step: 1,
                    Action: 'view',
                    Func: () => [
                        async (req, res, next) => {
                            res.updateCURD('vm', 'R', 0, (op) => {
                                op.ctx.options = {
                                    ...op.ctx.options,

                                    populate: [
                                        {
                                            path: 'UsedBy',
                                            select: 'UserName PhoneNumber Profile.Name',
                                        },
                                    ],
                                };
                            });
                            return next();
                        },
                        app.FindDocuments('', false, (req, res) => {
                            const vm = res.locals.data;

                            if (vm.UsedBy) {
                                // vm.using = true;

                                vm.UsedBy = (vm.UsedBy.Profile && vm.UsedBy.Profile.Name) || vm.UsedBy.UserName || vm.UsedBy.PhoneNumber;
                            }
                        }),
                    ]
                },
            ]);
        },
        onRoutersReady: async (app) => {
            app.get('/api/vm/list', 
                async (req, res, next) => {
                    res.updateCURD('vm', 'R', 0, (op) => {
                        op.ctx.options = {
                            ...op.ctx.options,

                            populate: [
                                {
                                    path: 'UsedBy',
                                    select: 'UserName PhoneNumber Profile.Name',
                                },
                            ],
                        };
                    });
                    return next();
                },
                app.FindDocuments('', false, (req, res) => {
                    const vms = res.locals.data.docs;

                    for (let i = 0; i < vms.length; i += 1) {
                        const vm = vms[i];
                        if (vm.UsedBy) {
                            // vm.using = true;

                            vm.UsedBy = (vm.UsedBy.Profile && vm.UsedBy.Profile.Name) || vm.UsedBy.UserName || vm.UsedBy.PhoneNumber;
                        }
                    }
                }),
            )
        },
    },
}